#!/bin/sh /etc/rc.common
# Copyright (c) 2011-2015 OpenWrt.org

START=90
USE_PROCD=1

start_instance() {
	config_get_bool enabled $1 enabled 0
	config_get autoactivate $1 autoactivate 1
	[ $enabled -eq 0 ] && return 0

	HOSTNAME=$(uci get system.@system[0].hostname)
	host_name=$(awk -F ',' '/^[ \t]*srv-host=_vlmcs\._tcp/{print $2}' /etc/dnsmasq.conf)

	echo $HOSTNAME
	echo $host_name

	if [ "$HOSTNAME" != "$host_name" ];then
		sed -i '/^[ \t]*srv-host=_vlmcs\._tcp/d' /etc/dnsmasq.conf
		sed -i '$a\srv-host=_vlmcs\._tcp,'"$HOSTNAME"','"1688"',0,100' /etc/dnsmasq.conf
		/etc/init.d/dnsmasq restart >/dev/null 2>&1
	fi

	procd_open_instance
	procd_set_param command /usr/bin/vlmcsd -D -i /etc/vlmcsd.ini -L 0.0.0.0:1688 -L [::]:1688
	procd_append_param command -l syslog

	procd_set_param respawn

	procd_set_param file /etc/vlmcsd.ini
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}

start_service() {
	config_load vlmcsd
	config_foreach start_instance vlmcsd
}

service_triggers() {
	procd_add_reload_trigger "vlmcsd"
}
